/*
 * @(#)RolepickerTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.taglib.html;

import java.sql.Connection;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.struts.util.RequestUtils;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.WebUtil;
import com.dcivision.user.bean.UserRole;
import com.dcivision.user.dao.UserRoleDAObject;

/**
  RolepickerTag.java

  @author          Jenny Li
  @company         DCIVision Limited
  @creation date   28/10/2004
  @version         $Revision: 1.2.32.5 $
 */
public class RolepickerTag extends TextTag {

  public static final String REVISION = "$Revision: 1.2.32.5 $";


  /** default constructor
   */
  public RolepickerTag() {
    super();
  }

  public int doStartTag() throws JspException {
    FormTag formTag = (FormTag)BodyTagSupport.findAncestorWithClass(this, FormTag.class);
    Object valueObj = RequestUtils.lookup(pageContext, name, property, null);
    String value = valueObj==null? "": valueObj.toString();
    String sPropertyName = this.getProperty();
    int retValue;

    // set the display value
    if ( Utility.isEmpty(value) ) {
      setValue("");
    } else {
      try {
        UserRole userRole = getUserRoleByID(new Integer(value));
        setValue(userRole.getRoleName());
      } catch (ApplicationException appEx) {
        throw new JspException(appEx);
      }
    }
    setProperty("dummy_" + sPropertyName);
    setOnkeypress("return false;");   // disable keyboard input
    setOnkeydown("return false;");    // disable keyboard input
    retValue = super.doStartTag();
    setProperty(sPropertyName);

    try{
      JspWriter out = pageContext.getOut();

      // hidden field
      out.println("<input type='hidden' name='" + sPropertyName + "' value='" + value + "'>");

      // show the user picker if it is for editing
      if (!textOnly && !formTag.getTextOnly()) {
        String contextPath = (String)this.pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY);
        String sUrl = contextPath + "/user/PopupUserRole.do?mode=L&property=" + sPropertyName + "&form=" + formTag.getBeanName();
        String sName = "DCIVisionPopupGroup";
        String sPopW = "700";
        String sPopH = "500";
        String sFeature = "scrollbars=yes,resizable=yes";

        // <a> tag for displaying the user picker
        out.print(" <a href=\"javascript:void(0);\" onclick=\"return OpenCenteredPopup('" + sUrl + "', '" + sName + "', " + sPopW + ", " + sPopH + ", '" + sFeature + "');\">");

        //print the html img tag for search user image
        out.print("<img ");
        out.print("src=\""+WebUtil.getImage(pageContext.getRequest(), "IconSearchRole.gif")+"\" ");
        out.print("align=\"absmiddle\" ");
        out.print("border=\"0\" ");
        out.print(">");

        // close the <a> tag
        out.println("</a>");
      }
    } catch (Exception e) {
      throw new JspException(e);
    }
    return retValue;
  }

  /** return a user record according to the given id
   *  @param    id
   *  @return   UserRecord
   *  @throws   ApplicationException
   */
  protected UserRole getUserRoleByID(Integer id) throws ApplicationException {
    SessionContainer sessionCon = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    Connection conn = (Connection)this.pageContext.getRequest().getAttribute(GlobalConstant.DB_KEY);
    UserRoleDAObject dao = new UserRoleDAObject(sessionCon, conn);

    try {
      return (UserRole)dao.getObjectByID(id);
    } catch (ApplicationException appEx) {
      throw appEx;
    }
  }

}
